%%
getSession;
sess.connect_device;
zi=sess.getDevice('dev3538');
%%
sol=Solstis.getInstance('192.168.1.222', 39933);
sol.OpenTCPIP;
%%
center=384.230588;
minDetune = -0.070; maxDetune = 0.070;
nFreq = 101; k = 1;

detuningList = linspace(minDetune, maxDetune, nFreq);
val=nan(size(detuningList));
fig=figure; ax = axes(fig);
%%
for detuning = detuningList
    freqOut=gotoFreq(sol, center+detuning);
    fprintf('k = %d, done.\n', k);
    pause(15);
    val(k) = mean(getPDSignal(zi));
    pause(1);
    fprintf('Freq= %9.6f\n\n', freqOut);
    plot(ax, detuningList, val, 'bo-'); grid on;
    k = k + 1;
end

function freqOut = gotoFreq(sol, freq_in_THz)
    freq_in_GHz = freq_in_THz*1e3;
    sol.GoToWL(299792458.0/freq_in_GHz) ;
    [~, wl] = sol.GetWL();
    freqOut = 299792458.0/wl/1000;
end

function sig = getPDSignal(zi)
    sig = zeros(1, 10);
    for k = 1:10
        sig(k) = zi.getAuxInValue('0');
        pause(0.1);
    end
end